﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Permissions;
using System.Security.Principal;
using System.Web.Security;
using System.Threading;
using KeanuMvc.Reference;
using HIS.Utils.Securities;
using System.IO;
using HIS.Utils;
using HISModels.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Configuration;


namespace HIS.Controllers.Schedule {
    public class SchedulerView : HIS.Controllers.AppBaseController {

        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private StringReader stringReader = null;
        private string doctorid = string.Empty;
        public string title { get; set; }

        #endregion

        public SchedulerView() {

        }

        [Authorization(RoleAction = "schedule")]
        public override void Index() {
            try {
                Data.Add("Pagetitle", "Doctor Schedule");
                RenderFile("Schedule\\schedulerdoctorview.html");

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        #region Bind to Table
         [Authorization(RoleAction = "schedule")]
        public void TableListDoctor() {
            try {
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];
                string doctorname = HttpContext.Current.Request["doctorname"];
                string code = HttpContext.Current.Request["code"];
                string specialist = HttpContext.Current.Request["specialist"];

                string ssearch = "specialist=" + specialist + ";doctorname=" + doctorname + ";code=" + code;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["doctorpaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                ///Convert to JObject
                ///  
                JObject o = JObject.Parse(stringReader.ReadToEnd());
                JArray aaData = (JArray)o["aaData"];
                ///Modify it
                JArray aaDataModified = new JArray();
                for (int i = 0; i < aaData.Count(); i++) {
                    JObject oo = JObject.Parse(aaData[i].ToString());
                    oo["Id"] = KeanuMvc.Reference.CryptoString.Encrypt(oo["Id"].ToString());
                    //oo["DoctorCode"] = (oo["DoctorCode"]).ToString();
                    //oo["DoctorName"] = (oo["DoctorName"]).ToString();
                    //oo["Specialist"] = (oo["Specialist"]).ToString();
                    aaDataModified.Add(oo);

                }
                o["aaData"] = aaDataModified;

                ///So serialize
                strJson = JsonConvert.SerializeObject(o);

                stringReader.Dispose();
                base.RestService.Dispose();
                HttpContext.Current.Response.Write(strJson);

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        //bind to datatable
         [Authorization(RoleAction = "schedule")]
        public void TableListOneWeek(){

            ///Set variables///
            try {
                ///Set Rest Url///
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];
                string doctorname = HttpContext.Current.Request["id"];

                string ssearch = "id=" + doctorname;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sSearch = ssearch;
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);

                string strUri = Global.ServiceUriDictionary["doctorschedulepagingcal"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));

                strJson = stringReader.ReadToEnd();

                HttpContext.Current.Response.Write(strJson);
            

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();

            }
        }

        #endregion

        #region Bind to Schedule Page

        //render to schedule function 
         [Authorization(RoleAction = "schedule")]
        public void ScheduleDoctor() {
            string strJson = string.Empty;
            string DoctorName = string.Empty;
            string GroupSpecialist = string.Empty;
            string SubSpecialist = string.Empty;
            try {
                Data.Add("Pagetitle", "Doctor Schedule");

                doctorid = KeanuMvc.Reference.CryptoString.Decrypt(HttpContext.Current.Request["id"].NullSafeToString());
                string strUri = Global.ServiceUriDictionary["doctor"] + doctorid + Global.ServiceUriDictionary["requestformat"];

                #region Render to another page

                stringReader = base.RestService.GetJson(strUri);

                JObject o = JObject.Parse(stringReader.ReadToEnd());
                JArray aaData = (JArray)o["Detail"];

                JArray aaDataModified = new JArray();
                for (int i = 0; i < aaData.Count(); i++) {
                    JObject oo = JObject.Parse(aaData[i].ToString());
                    DoctorName = (oo["doctorname"]).ToString();
                    GroupSpecialist = (oo["groupspecialist"]).ToString();
                    SubSpecialist = (oo["subspecialist"]).ToString();
                    aaDataModified.Add(oo);

                }
                o["aaData"] = aaDataModified;

                Data.Add("DoctorName", DoctorName);
                Data.Add("GroupSpecialist", GroupSpecialist);
                Data.Add("SubSpecialist", SubSpecialist);
                Data.Add("DoctorId", doctorid);
                ///So serialize
                strJson = JsonConvert.SerializeObject(o);

                stringReader.Dispose();
                base.RestService.Dispose();

                #endregion

                RenderFile("schedule\\schedulerdoctorcalendarview.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        #endregion

    }
}